﻿#include "logindialog.h"
#include "ui_logindialog.h"
#include <QRegExp>
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QDir>

LoginDialog::LoginDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::LoginDialog)
{
    qDebug() << "Current Path:" << QDir::currentPath();
    ui->setupUi(this);
    setWindowTitle("driver_exam_sys");
    setWindowFlags(Qt::Dialog|Qt::WindowCloseButtonHint);
    resize(ui->label_bg->width(),ui->label_bg->height());
    ui->label_bg->move(0,0);

}

LoginDialog::~LoginDialog()
{
    delete ui;
}


void LoginDialog::on_pushButton_log_clicked()
{
    QRegExp exp("^[a-zA-Z0-9]{6,18}@[a-z]{,3}.[a-z]{,3}");
    QString account;
    QString password;
    account=ui->lineEdit_account->text();
    password=ui->lineEdit_password->text();
    int res=exp.exactMatch(account);
    if(!res)
    {
        QMessageBox::information(this,"提示","用户名格式错误!请重新输入:");
        ui->lineEdit_account->clear();
        ui->lineEdit_password->clear();
        ui->lineEdit_account->setFocus();
        return;
    }
    else
    {
        if(password.isEmpty())
        {
            QMessageBox::information(this,"提示","密码不能为空,请输入:");
            ui->lineEdit_password->setFocus();
            return;
        }

        //读取文件
        QString filename("../account.txt");
        QFile file(filename);
        QTextStream textStream(&file);

        if(file.open(QIODevice::ReadOnly|QIODevice::Text))
        {
            QString strLine;
            QStringList strList;
            while (1)
            {
                strLine=textStream.readLine();
                if(textStream.atEnd())
                {
                    break;
                }
                strList=strLine.split(",");
                if(strList.size()<2)
                {
                    QMessageBox::information(this,"提示","读取用户名或密码失败");
                    file.close();
                    return ;
                }
                if(strList[0]==account)
                {
                    if(strList[1]==password)
                    {
                        QMessageBox::information(this,"提示","密码正确,欢迎大佬!");
                        done(Accepted);
                        file.close();
                        return ;
                    }
                    else
                    {
                        QMessageBox::information(this,"提示","密码错误,请重新输入:");
                        ui->lineEdit_password->clear();
                        ui->lineEdit_password->setFocus();
                        file.close();
                        return ;
                    }
                }
            }

            QMessageBox::information(this,"提示","账户错误,请重新输入:");
            ui->lineEdit_account->clear();
            ui->lineEdit_password->clear();
            ui->lineEdit_account->setFocus();
            file.close();
            return;

        }
        else
        {
            QMessageBox::information(this,"提示","文件读取失败!");
            file.close();
            return;
        }

    }
}

void LoginDialog::on_pushButton_cal_clicked()
{
    done(Rejected);
}
